// const formatTime = date => {
//   const year = date.getFullYear()
//   const month = date.getMonth() + 1
//   const day = date.getDate()
//   const hour = date.getHours()
//   const minute = date.getMinutes()
//   const second = date.getSeconds()

//   return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
// }

// const formatNumber = n => {
//   n = n.toString()
//   return n[1] ? n : `0${n}`
// }

/**
 * @description: 将时间戳转换为指定格式
 * @param {*} time 时间戳
 * @param {*} cFormat 时间格式
 * @return {*}
 */
const parseTime = (time, cFormat) => {
  if (arguments.length === 0) {
    return null;
  }
  const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}

/**
 * @description: 计算当前时间距离已过去某个时间的差值
 * @param {*} time 时间戳
 * @param {*} option 时间格式
 * @return {*}
 */
const formatTime = (time, option) => {
  if (("" + time).length === 10) {
    time = parseInt(time) * 1000;
  } else {
    time = +time;
  }
  const d = new Date(time);
  const now = Date.now();

  const diff = (now - d) / 1000;

  if (diff < 30) {
    return "刚刚";
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + "分钟前";
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + "小时前";
  } else if (diff < 3600 * 24 * 2) {
    return "1天前";
  }
  if (option) {
    return parseTime(time, option);
  } else {
    return (
      d.getMonth() +
      1 +
      "月" +
      d.getDate() +
      "日" +
      d.getHours() +
      "时" +
      d.getMinutes() +
      "分"
    );
  }
}

/**
 * @description: 隐藏手机号中间四位数为****
 * @param {*} tel 手机号
 * @return {*}
 */
const formatPhone = (tel) => {
  var reg = /^(\d{3})\d{4}(\d{4})$/;
  return tel.replace(reg, "$1****$2");
}

/**
 * @description: 防抖
 * @param {*} fun 自定义函数
 * @param {*} time time秒之内重复点击，只会执行最后一次自定义函数
 * @return {*}
 */
const debounce = (fun, time) => {
  let timer;
  return function () {
    clearTimeout(timer); //time秒内再次触发fun函数时则清空上一次的计时并重新计时
    let args = arguments;
    timer = setTimeout(() => {
      fun.apply(this, args); //改变fun函数内部this指向并处理fun函数的参数
    }, time);
  };
}

/**
 * @description: 节流
 * @param {*} fun 自定义函数
 * @param {*} time time秒之内重复点击，只会执行一次自定义函数
 * @return {*}
 */
const throttle = (fun, time) => {
  let t1 = 0; //初始时间
  return function () {
    let t2 = new Date(); //当前时间
    if (t2 - t1 > time) {
      //如果当前时间距离上一个时间也就是初始时间大于所设置的time时，执行fun函数并把初始时间变更为这一次执行的时间
      fun.apply(this, arguments); //改变fun函数内部this指向并处理fun函数的参数
      t1 = t2;
    }
  };
}

module.exports = {
  parseTime,
  formatTime,
  formatPhone,
  debounce,
  throttle
}